<?php
namespace app\mobile\controller;
use app\common\model\CardCzgive;
use app\common\model\CardMember;
use app\common\model\CardQrcode;
use app\common\model\CardUserGet;
use app\common\model\CardRank;
use app\common\model\Agent;
use app\common\model\Paylog;
use app\common\model\WxOpen;
use app\common\model\Merchant;
use app\common\model\System;
use think\controller;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;
class Cardconsume extends controller
{
    //获取核销人员信息
    public function getconuser()
    {
        $openid = Session::get('openid');
        $sysuser = new WxUser();
        $conuser = $sysuser->where('openid', $openid)->find();
        //判断是否属于核销员用户组
        if ($conuser['groupid'] == 1 && $conuser > 0) {
            return $this->fetch('');
        } else {

        }
    }

    //会员卡核销登录扫码
    public function index()
    {
        $openid = Session::get('openid');
        $sysuser = new WxUser();
        $conuser = $sysuser->where('openid', $openid)->find();
        //判断是否属于核销员用户组
        if ($conuser['groupid'] == 1) {
            if ($conuser['mid'] == 0){
                die("<script>alert('请先绑定门店')</script>");
                $this->redirect('users/offweb');
            }else{
                return $this->fetch('');
            }
        } else {
            die("<script>alert('请使用核销员微信登录')</script>");
            $this->redirect('users/offweb');
        }
    }


    public function updatecard()
    {

        $request = request();
        if ($request->isAjax()) {
            $users = $request->post();
            $sysuser = new WxUser();
            $wxcardget = new WxCardsGet();
            $cardupdate = new WxCardUpdate();
            $usercon = Session::get();
            $userinfo = $wxcardget->where('code', $users['code'])->find();
            $suserinfo = $sysuser->where('openid', $usercon['openid'])->find();
            if (md5($users['password']) == $userinfo['password']) {
                $del_balance = round($users['del_balance'],2);
                $balance = round($userinfo['balance'],2);
                if ( $balance >= $del_balance ){
                    $map['id'] = $userinfo['id'];
                    $wxcardget->where($map)->setDec('balance',$users['del_balance']);
                    $datas['card_id']=$userinfo['card_id'];
                    $datas['openid']=$userinfo['openid'];
                    $datas['con_openid']=$suserinfo['openid'];
                    $datas['del_balance']=$users['del_balance'];
                    $datas['code']=$users['code'];
                    $datas['mid']=$suserinfo['mid'];
                    $datas['source'] = $users['source'];
                    $res = $cardupdate->save($datas);
                    if ($res == false) {
                        Session::delete('code');
                        $data = array('code' => 2, 'message' => "核销失败");
                    } else {
                        //发送模版消息
                        $arr = ['balance' => $users['del_balance'],'code'=> $users['code'],'openid' => $userinfo['openid'],'mid' => $suserinfo['mid']];
                        $this->mesgTemp($arr);

                        Session::delete('code');
                        $data = array('code' => 0, 'message' => "核销成功");
                    }
                }else{
                    $data = array('code' => 2, 'message' => "余额不足");
                }
            } else {
                $data = array('code' => 1, 'message' => "密码错误");
            }
            return json($data);
        } else {
            $usercon = Session::get();
            if (isset($usercon['openid'])) {
                if (isset($usercon['code'])) {
                    $CardUserGet = new CardUserGet();
                    $CardMember = new CardMember();
                    $userinfo = $CardUserGet->where('code', $usercon['code'])->find();
                    $userinfo['logo_url'] = $CardMember->where('card_id', $userinfo['card_id'])->value('logo_url');
                    $this->assign('user', $userinfo);
                    return $this->fetch('');
                } else {
                    die("<script>alert('没有查询到该会员的相关信息')</script>");
                }

            } else {
                die("<script>alert('您没有权限')</script>");
            }
        }
    }

    public function getcode()
    {
        $request = request();

        if ($request->isAjax()) {
            $code = $request->post('code');
            Session::set('code', $code);
            $data = 0;

        } else {
            $data = 1;
        }
        return json($data);
    }

    //发送模版消息
    public function mesgTemp($arr)
    {




    }

    //JSSDK
    public function jssdk()
    {
        return $this->fetch();
    }
}
